#!/bin/bash -e

function info() {
echo Usage: `basename $0` 'name ip'
exit 1
}

while getopts  ":p:f:" opt; do
	case  $opt  in
		p) out_prefix=$OPTARG;;
		f) suffix=$OPTARG;;
		*) info;;
	esac
done
shift $(($OPTIND - 1))


if [ $# -lt 1 ]; then info; fi

. ~/virtual/web/bin/activate

dir=`pwd`
name=$1

mkdir -p $name/$name/static
mkdir -p $name/$name/templates && cd $name

cat << setup > setup.py
from setuptools import setup

setup(
    name='$name',
    packages=['$name'],
    include_package_data=True,
    install_requires=[
        'flask',
    ],
)
setup

cd $name

test=test
blueprint=bpt
blueprint_file=bpt_test

cat << init > __init__.py
from flask import Flask
app = Flask(__name__)

import $name.$test
init


cat << name > $test.py
from $name import app
from $name.$blueprint_file import $blueprint

@app.route('/')
def index():
    return 'Hello World!'
    
app.register_blueprint($blueprint)

name

cat << blueprint > $blueprint_file.py
from flask import Blueprint, render_template, abort
from jinja2 import TemplateNotFound

$blueprint = Blueprint('test_blueprint', __name__,
                        template_folder='templates')

@$blueprint.route('/bpt', defaults={'page': 'index'})
@$blueprint.route('/bpt/<page>')
def show(page):
    try:
        return render_template('%s.html' % page)
    except TemplateNotFound:
        abort(404)
blueprint

:>static/style.css
echo index >templates/index.html
echo login >templates/login.html
echo logout >templates/logout.html


cd $dir/$name
pip install -e .


export FLASK_APP=$name # absolute path seems wrong
export FLASK_DEBUG=true

ip="-h $2"
flask run $ip


